using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Ent.Voice;
using Ent.Web.Helper;

public partial class Member_Message : System.Web.UI.Page
{
    int count = 8;
    protected void Page_Load(object sender, EventArgs e)
    {
        btnDelete.Style.Add(HtmlTextWriterStyle.Display, "none");
        if (Session["USERID"] == null)
            return;
        PopulateList();
        SetJavascript();
    }
    private void PopulateList()
    {
        try
        {
            int pi = MathFunction.ConvertToInteger(Request.QueryString["pi"], 0);
            pi = pi == 0 ? 1 : pi;
            int start = (pi - 1) * count + 1;

            int total = 1;
            DataTable dt = GetMessages(ref total, start);
            int pageCount = dt.Rows.Count;
            lblHeader.Text = ResourceHelper.Get("MSG_SearchHeader") + " " + start.ToString() + " to " + Convert.ToInt32(start + pageCount - 1) + " of " + total.ToString() + " records.";
            SetNavigation(pi, total, start);
            if (dt != null && dt.Rows.Count != 0)
            {
                dgMessageList.DataSource = dt;
                dgMessageList.DataBind();
            }
            else
            {
                lblHeader.Text = Common.GetAlertText(ResourceHelper.Get("MSG_NoMessage"));
                dgMessageList.Visible = false;
            }
        }
        catch (Exception exc)
        {
            Common.ErrorPage(exc);
        }
    }
    private void SetNavigation(int pi, int total, int start)
    {
        Dictionary<string, string> kvList = new Dictionary<string, string>();
        kvList.Add("pi", "");
        btnPrev.Visible = false;
        btnNext.Visible = false;
        if (pi > 1)
        {
            btnPrev.Visible = true;
            kvList["pi"] = Convert.ToString(pi - 1);
            btnPrev.Attributes.Add("onclick", "javascript:window.location.href='" + Common.ResetUrl(kvList) + "'");
        }
        if (total >= start + count)
        {
            btnNext.Visible = true;
            kvList["pi"] = Convert.ToString(pi + 1);
            btnNext.Attributes.Add("onclick", "javascript:window.location.href='" + Common.ResetUrl(kvList) + "'");
        }
    }
    private DataTable GetMessages(ref int total, int startIndex)
    {
        DataTable retValue = new DataTable();
        retValue.Columns.Add("Id");
        retValue.Columns.Add("Subject");
        retValue.Columns.Add("Content");
        retValue.Columns.Add("FromUserId");
        retValue.Columns.Add("PostDate");
        retValue.Columns.Add("IsRead");
        retValue.Columns.Add("InReference");
        retValue.Columns.Add("FromUserName");
        Message[] ms = Message.GetUserMessages(Session["USERID"].ToString(), startIndex, count, ref total);
        if (ms == null)
            return retValue;
        for (int i = 0; i < ms.Length; i++)
        {
            Message m = (Message)ms.GetValue(i);
            if (m != null)
            {
                DataRow dr = retValue.NewRow();
                dr["Id"] = m.Id;                        // 0
                dr["Subject"] = m.Subject;              // 1
                dr["Content"] = m.Content;              // 2
                dr["FromUserId"] = m.FromUserId;        // 3
                dr["PostDate"] = m.PostDate;            // 4
                dr["IsRead"] = m.IsRead;                // 5
                dr["InReference"] = m.InReference;      // 6
                dr["FromUserName"] = m.FromUserName;    // 7
                retValue.Rows.Add(dr);
            }
        }
        return retValue;
    }
    protected void dgMessageList_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex >= 0)
        {
            string str = "";
            str = "<table id=\"mrow" + e.Item.Cells[0].Text + "\" cellpadding='0' cellspacing='0' class='" + (Boolean.Parse(e.Item.Cells[5].Text) ? "message_RowRead" : "message_RowUnread") + "'>";
            str += "<tr>";
            str += "<td class='message_RowImage'><img style=\"width:15px;height:15px;\" src='" + ConfigurationManager.AppSettings["BaseUrl"] + "/Images/dj/mail.gif' /></td>";
            str += "<td class='message_RowData'>" + Common.GetImageTextLink("ShowUser.aspx?id=" + e.Item.Cells[3].Text, "Images/dj/user.gif", StringHelper.RestrictMaxChar(e.Item.Cells[7].Text, 20), 15);
            //<a href=\"" + ConfigurationManager.AppSettings["BaseUrl"] + "/ShowUser.aspx?id=" + e.Item.Cells[3].Text + "\">" + StringHelper.RestrictMaxChar(e.Item.Cells[7].Text, 20) + "</a>";
            str += "&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"message_RowDate\">" + DateTime.Parse(e.Item.Cells[4].Text).ToShortDateString() + "</span>";
            str += "<br />";
            str += "<span class=\"message_RowContent\" onclick=\"javascript:ShowMessages(" + e.Item.Cells[0].Text + ");\">" + StringHelper.RestrictMaxChar(e.Item.Cells[1].Text + ": " + e.Item.Cells[2].Text, 50) + "</span>";
            str += "<span class=\"message_RowReply\"><a href=\"" + ConfigurationManager.AppSettings["BaseUrl"] + "/SendMessage.aspx?id=" + e.Item.Cells[3].Text + "\"><img style=\"border:none;width:15px;height:15px;\" src='" + ConfigurationManager.AppSettings["BaseUrl"] + "/Images/dj/feed.gif' /></a><span>";
            str += "&nbsp;<span class=\"message_RowDelete\"><img style=\"border:none;width:15px;height:15px;\" src=\"" + ConfigurationManager.AppSettings["BaseUrl"] + "/Images/dj/err.gif\" "
                + " onclick=\"javascript:document.getElementById('"
                + hfMessageId.ClientID + "').value='" + e.Item.Cells[0].Text + "'; __doPostBack('"
                + btnDelete.UniqueID + "','')\"" 
                + " /><span>";
            str += "</td>";
            str += "</tr>";
            str += "</table>";
            e.Item.Cells[0].Text = str;
        }
        e.Item.Cells[1].Visible = false;
        e.Item.Cells[2].Visible = false;
        e.Item.Cells[3].Visible = false;
        e.Item.Cells[4].Visible = false;
        e.Item.Cells[5].Visible = false;
        e.Item.Cells[6].Visible = false;
        e.Item.Cells[7].Visible = false;

    }
    private void SetJavascript()
    {
        string jScript = "<script type=\"text/javascript\">";
        jScript += "function ShowMessages(id) {";
        jScript += "\r\ndocument.getElementById('mrow' + id).className = 'message_RowRead';";
        jScript += "\r\nvotewindow=dhtmlmodal.open('RuleSetBox', 'iframe', 'ShowMessage.aspx?id=' + id, 'Message', 'width=560px,height=410px,center=1,resize=0,scrolling=0')";
        jScript += "\r\nreturn false;";
        jScript += "\r\n}";
        jScript += "var siteurl='" + ConfigurationManager.AppSettings["BaseUrl"] + "'";
        jScript += "\r\n</script>";
        Page.RegisterClientScriptBlock("script_name", jScript);
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        int id = -1;
        int.TryParse(hfMessageId.Value, out id);
        if (id != -1)
        {
            Message.DeleteMessage(id, StringHelper.ConvertToString(Session["USERID"]));
            PopulateList();
        }
    }
}
